<template>
  <div>
    <el-form ref="form">
      <el-form-item prop="search" class="mb20 mt10">
        <el-col :span="4">
          <el-button type="primary" @click="handleAdd" icon="el-icon-circle-plus-outline">添加业务员</el-button>
        </el-col>
        <el-row type="flex" class="row-bg" justify="end">
          <el-col :xs="12" :sm="10" :md="8" :lg="6">
            <el-input
              v-model="search"
              size="mini"
              placeholder="输入业务员姓名搜索"
              @keyup.enter.native="getSalesmans(15, 0)"
            ></el-input>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-search"
              @click="getSalesmans(15, 0)"
            >搜索</el-button>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-circle-close-outline"
              @click="resetSearch"
            >重置</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <salesmanDialog
      :title="title"
      :addDialog="addDialog"
      :editDialog="editDialog"
      :salesmanForm="salesmanForm"
      @close="hanleClose"
      @update="handleUpdate"
    ></salesmanDialog>
    <el-table
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column type="selection" width="65"></el-table-column>
      <el-table-column prop="id" label="序号" width="65"></el-table-column>
      <el-table-column sortable prop="createdAt" label="创建时间" width="180">
        <template slot-scope="scope">{{scope.row.createdAt | moment("YYYY/MM/DD HH:mm:ss")}}</template>
      </el-table-column>
      <el-table-column prop="centerName" label="中心名" width="180"></el-table-column>
      <el-table-column prop="deptName" label="部门名" width="180"></el-table-column>
      <el-table-column prop="name" label="业务员名" width="180"></el-table-column>
      <el-table-column prop="operation" label="操作" width="320">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
            icon="el-icon-edit-outline"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            icon="el-icon-delete"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="mt20">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pages"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import salesmanDialog from "@/components/salesman_dialog";

export default {
  name: "salesman",
  data() {
    return {
      page: 15,
      total: 0,
      search: "",
      currentPage: 1,
      pages: [15, 20, 25, 35],
      title: "",
      page: 15,
      val: 1,
      tableData: [],
      loading: true,
      addDialog: false,
      editDialog: false,
      salesmanForm: {
        name: ""
      }
    };
  },
  created() {
    this.getSalesmans(15, 0);
  },
  methods: {
    handleSizeChange(val) {
      this.page = val;
      this.getSalesmans(val, 0);
    },
    handleCurrentChange(val) {
      this.val = val;
      // 下一页页数*每页显示行-上一页页数*每页显示行
      this.getSalesmans(this.page, (val - 1) * this.page);
    },
    getSalesmans(limit, offset) {
      this.$axios
        .get(
          `/api/system/salesmans/?limit=${limit}&offset=${offset}&search=${
            this.search
          }`
        )
        .then(data => {
          this.tableData = data.doc;
          this.total = data.total;
          this.loading = false;
        });
    },
    handleAdd() {
      this.addDialog = !this.addDialog;
      this.title = "添加业务员";
    },
    handleEdit(item) {
      this.salesmanForm = item;
      this.editDialog = !this.editDialog;
      this.title = "编辑业务员";
    },
    handleDelete(id) {
      this.$confirm("此操作将删除该业务员, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios.delete("/api/system/removeSalesman/" + id).then(data => {
            const { status } = data;
            if (!status) {
              this.$message({
                type: "warning",
                message: data.msg
              });
              return;
            }
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.handleUpdate();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    hanleClose() {
      this.addDialog = false;
      this.editDialog = false;
      this.resetForm();
    },
    handleUpdate() {
      this.getSalesmans(this.page, (this.val - 1) * this.page);
    },
    resetSearch() {
      this.search = "";
      this.getSalesmans(15, 0);
    },
    handleSearch() {
      const search = {
        search: this.search
      };
      this.$axios.post("/api/system/salesmanSearch/", search).then(data => {
        this.tableData = data;
      });
    },
    resetForm() {
      this.salesmanForm = {};
    }
  },
  components: {
    salesmanDialog
  }
};
</script>
<style>
</style>
